//
//  BuyItemCollectionView.swift
//  HappyToSee
//
//  Created by Wade on 2024/7/5.
//

import Foundation
import UIKit
import QMUIKit
class KEGameViewAnwserCell :UICollectionViewCell{
    var anwserBtn:UIButton?
    var indexLabel:UILabel?
    var gameType:KEGameType? = .imageToWords
    var index:Int?{
        didSet{
            switch self.index {
            case 0:
                self.indexLabel?.text = "A."
            case 1:
                self.indexLabel?.text = "B."
            case 2:
                self.indexLabel?.text = "C."
            case 3:
                self.indexLabel?.text = "D."
            default:
                print("")
            }
        }
    }
    var wordDetail:KEWordsDetail? {
        get{
            return _wordDetail
        }
        set{
            _wordDetail = newValue
            anwserBtn?.setImage(nil, for: .normal)
            anwserBtn?.setTitle(nil, for: .normal)
            anwserBtn?.imageView?.contentMode = .scaleAspectFill
            switch self.gameType {
            case .imageToWords:
                anwserBtn?.setTitle(wordDetail?.word_en, for: .normal)
            case .wordToImage:
                anwserBtn?.sd_setImage(with: .init(string: wordDetail?.word_iamges?.first ?? ""), for: .normal)
            case .imageToAudio:
                anwserBtn?.setImage(.init(named: "ic_audio_player"), for: .normal)
            case .audioToimage:
                anwserBtn?.sd_setImage(with: .init(string: wordDetail?.word_iamges?.first ?? ""), for: .normal)
               
            case .audioToWord:
                anwserBtn?.setTitle(wordDetail?.word_en, for: .normal)
            case .wordToAudio:
                anwserBtn?.setImage(.init(named: "ic_audio_player"), for: .normal)
            default:
                print("")
            }

           
        }
    }
    private var _cellSelect:Bool = false
    private var _wordDetail:KEWordsDetail?
    var setCellSelect:Bool  {
        get{
            return _cellSelect
        }
        
        set{
            _cellSelect = newValue
            if _cellSelect == false{
                let color = UIColor.qmui_color(withHexString: "#E2B86B")
                self.indexLabel?.textColor = color
              
                
            }else{
                let color = UIColor.qmui_color(withHexString: "#46310D")
                self.indexLabel?.textColor = color
            }
        }
    }
    
    
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupSubViews()
    }
    
    func setupSubViews(){
        self.backgroundColor = .clear;
        
        let label1 = UILabel()
        label1.textColor = UIColor.qmui_color(withHexString: "#333")
        label1.textAlignment = .center
        label1.font = .boldSystemFont(ofSize: 20)
        label1.text = "A."
        self.addSubview(label1)
        label1.snp.makeConstraints { make in
            make.width.height.equalTo(25)
            make.top.equalTo(self).offset(10)
            make.left.equalTo(self).offset(10)
        }
        self.indexLabel = label1
        
        
        let anwserBtn = UIButton()
        self.addSubview(anwserBtn)
        self.anwserBtn = anwserBtn
        anwserBtn.titleLabel?.textAlignment = .center
        anwserBtn.titleLabel?.font = .systemFont(ofSize: 35)
        anwserBtn.titleLabel?.numberOfLines = 0;
        anwserBtn.setTitleColor(.black, for: .normal)
        anwserBtn.setImage(.init(named: "apple-pic"), for: .normal)
        anwserBtn.snp.makeConstraints { make in
            make.left.equalTo(label1.snp.right).offset(0)
            make.width.height.equalTo(self.qmui_width - 30)
            make.centerY.equalTo(contentView)
        }
        anwserBtn.addTarget(self, action: #selector(anwserBtnClick), for: .touchUpInside)
   
        
    }
    
    @objc func anwserBtnClick(){
        if(self.gameType == .imageToAudio || self.gameType == .wordToAudio){
            KEAudioManager.sharedInstance.playEn(en: self.wordDetail?.word_en ?? "", zh: "" )
        }
    }
    
    override func layoutSubviews() {
        
    }
}
